// pages/common/recorder.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    talking:false,
    deleteTalk:false,
    position:{
      pageX:null,
      pageY:null
    },
    tempFilePath:null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 改变大小
    startMove(e) {
      // console.log(e)
      this.setData({
        talking: true,
        deleteTalk: false,
        'position.pageX': e.touches[0].pageX,
        'position.pageY': e.touches[0].pageY,
        tempFilePath: wx.getRecorderManager()
      })
      this.data.tempFilePath.start()
      this.data.tempFilePath.onStop((res) => {
        const { tempFilePath } = res
        if (this.data.deleteTalk) {
          // console.log('已取消')
          this.setData({
            deleteTalk: false,
          })
          return
        }
        // console.log('录音文件：', res)
        this.triggerEvent("record", res)
      })
    },
    move(e) {
      // console.log(e)
      let index = e.currentTarget.dataset.index,
        pageX = e.touches[0].pageX,
        pageY = e.touches[0].pageY,
        startX = this.data.position.pageX,
        startY = this.data.position.pageY;
      if (startX === null || startY === null) {
        return;
      }
      this.setData({
        deleteTalk: pageX - startX > 60 ? true : false,
      })
    },
    endMove() {
      this.setData({
        talking: false,
        'position.pageX': null,
        'position.pageY': null,
      })
      this.data.tempFilePath.stop()
    },
  }
})
